{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyrb import EqualRiskContribution, RiskBudgeting, RiskBudgetAllocation\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#get a covariance matrix of an asset universe\n",
    "covariance_matrix = pd.read_csv(\"data.csv\",sep=\";\",index_col=0).pct_change().cov() * 260\n",
    "covariance_matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Solving the ERC problem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ERC = EqualRiskContribution(covariance_matrix)\n",
    "ERC.solve()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " The optimal solution that gives equal risk contributions is:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "optimal_weights =  ERC.x\n",
    "risk_contributions =  ERC.get_risk_contributions(scale = False)\n",
    "risk_contributions_scaled =  ERC.get_risk_contributions()\n",
    "allocation = pd.DataFrame(np.concatenate([[optimal_weights,risk_contributions,risk_contributions_scaled]]  ).T, index = covariance_matrix.index,columns=[\"optinal weigths\",\"risk contribution\",\"risk contribution(scaled)\"])\n",
    "allocation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each asset has a risk contribution of 10% to the total risk. We also verify that the sum of the risk budget is equal to the variance:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.round(np.dot(np.dot(ERC.x,covariance_matrix),ERC.x)**0.5,10) == np.round(allocation['risk contribution'].sum(),10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Solving the risk budgeting problem\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we want the risk contributions equal to specific budgets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "budgets = [0.1,0.1,0.1,0.2,0.2,0.05,0.05,0.05,0.05,0.1]\n",
    "RB = RiskBudgeting(covariance_matrix,budgets)\n",
    "RB.solve()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "optimal_weights =  RB.x\n",
    "risk_contributions =  RB.get_risk_contributions(scale = False)\n",
    "risk_contributions_scaled =  RB.get_risk_contributions()\n",
    "allocation = pd.DataFrame(np.concatenate([[optimal_weights,risk_contributions,risk_contributions_scaled]]  ).T, index = covariance_matrix.index,columns=[\"optinal weigths\",\"risk contribution\",\"risk contribution(scaled)\"])\n",
    "allocation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Again the risk contributions match the budgets and the variance equals the sum of the risk contribution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " np.round(np.dot(np.dot(RB.x,covariance_matrix),RB.x)**0.5,10) == np.round(allocation['risk contribution'].sum(),10)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
